#include "MyUtils.h"
#include <cassert>
#include <iostream>

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>

void MyUtils::Assert(bool value)
{
	if (!value) {
		__debugbreak();
	}
}

void MyUtils::Exit(const string& msg)
{
	cout << "Error:" << endl;
	cout << msg << endl;
	exit(1);
}


GLuint MyUtils::CreateTexture(const string& filepath)
{
	int x, y, n;
	unsigned char* data = stbi_load(filepath.c_str(), &x, &y, &n, 4);
	assert(data != NULL);
	fprintf(stdout, "width:%d,height:%d,bpp:%d\n", x, y, n);
	GLuint tex;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
	stbi_image_free(data);
	glBindTexture(GL_TEXTURE_2D, 0);
	return tex;
}